﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace LLFM.CAT.Config
{
   
    public sealed class ZijiaUrlManager
    {
        private ZijiaUrlManager() { ;}

        public static readonly string HostName = ConfigurationManager.AppSettings["HostName"].ToString();
        public static readonly string UrlSeparator = ConfigurationManager.AppSettings["UrlSeparator"].ToString();
        public static readonly string UrlExtend = ConfigurationManager.AppSettings["UrlExtend"].ToString();
        public static readonly string Domain = ConfigurationManager.AppSettings["Domain"].ToString();
        public static readonly string SubDomain = ConfigurationManager.AppSettings["SubDomain"].ToString();
        public static readonly string BaaDomain = ConfigurationManager.AppSettings["BaaDomain"].ToString();
        public static readonly string QiCheTongDomain = ConfigurationManager.AppSettings["QiCheTongDomain"].ToString();

        /// <summary>
        /// 主机头
        /// </summary>
        /// <returns></returns>
        public static string GetHost() { return HostName; }

        /// <summary>
        /// 子域名称
        /// </summary>
        /// <returns></returns>
        public static string GetSubDomain() { return string.Concat(SubDomain, ".", GetHost()); }

        /// <summary>
        /// 参数分隔符
        /// </summary>
        /// <returns></returns>
        public static string GetUrlSeparator() { return UrlSeparator; }

        /// <summary>
        /// url扩展名
        /// </summary>
        /// <returns></returns>
        public static string GetUrlExtend() { return UrlExtend; }

        /// <summary>
        /// 获得完整主机头
        /// </summary>
        /// <param name="protocol"></param>
        /// <returns></returns>
        public static string GetFullHostName()
        {
            return string.Concat("http://", GetHost(), GetUrlSeparator());
        }

        public static string GetFullHostName(string hostName)
        {
            return string.Concat("http://", hostName, GetUrlSeparator());
        }

        /// <summary>
        /// 获取含有子域的主机头
        /// </summary>
        /// <returns></returns>
        public static string GetFullSubHostName()
        {
            return string.Concat("http://", GetSubDomain(), GetUrlSeparator());
        }

        /// <summary>
        /// 获得完整主机头及虚拟目录
        /// </summary>
        /// <param name="protocol"></param>
        /// <returns></returns>
        public static string GetFullVirsualName(params string[] virsualPath)
        {
            string f = GetFullHostName();
            string sep = GetUrlSeparator();
            return string.Concat(f, string.Join(sep, virsualPath), GetUrlSeparator());
        }

        /// <summary>
        /// 获得含有子域的完整主机头及虚拟目录
        /// </summary>
        /// <param name="virsualPath">The virsual path.</param>
        /// <returns></returns>
        public static string GetSubDomainFullVirsualName(params string[] virsualPath)
        {
            string f = GetFullSubHostName();
            string sep = GetUrlSeparator();
            return string.Concat(f, string.Join(sep, virsualPath));
        }

        /// <summary>
        /// 获取一个自定义完整的Url
        /// </summary>
        /// <param name="protocol">协议名称</param>
        /// <param name="module">模块名称</param>
        /// <param name="ParameterSeparator">自定义参数名称</param>
        /// <param name="prams">参数值</param>
        /// <returns></returns>
        public static string GetFullPageName(string[] module
            , string ParameterSeparator
            , params string[] prams)
        {
            string relativeUrl = GetRelativeUrl(module, ParameterSeparator, prams);
            string host = GetFullHostName();
            return string.Concat(host, relativeUrl);
        }

        /// <summary>
        /// 获取一个自定义含有子域的完整的Url
        /// </summary>
        /// <param name="module">The module.</param>
        /// <param name="ParameterSeparator">The parameter separator.</param>
        /// <param name="prams">The prams.</param>
        /// <returns></returns>
        public static string GetSubDomainFullPageName(string[] module
            , string ParameterSeparator
            , params string[] prams)
        {
            string relativeUrl = GetRelativeUrl(module, ParameterSeparator, prams);
            string subhost = GetFullSubHostName();
            return string.Concat(subhost, relativeUrl);
        }

        /// <summary>
        /// 获取一个自定义的相对目录
        /// <code>白玉 2011-12-27</code>
        /// </summary>
        /// <param name="module">模块名称</param>
        /// <param name="ParameterSeparator">自定义参数名称</param>
        /// <param name="prams">参数值</param>
        /// <returns></returns>
        public static string GetRelativeUrl(string[] module, string ParameterSeparator, params string[] prams)
        {
            string mid = string.Join(GetUrlSeparator(), module);
            return prams == null || prams.Length == 0
                ? string.Concat(mid, GetUrlExtend())
                : string.Concat(mid, ParameterSeparator, string.Join(ParameterSeparator, prams), GetUrlExtend());
        }
    }
}
